import getProp from './prop';
import { validateRequired, validateType } from '../rules/utils';
import { validateStrLen } from '../rules/string';

const TIPS_TEXT = '输入';
/**
 * 获取验证字段
 * @param {*} { fields }
 * @return {*} rule
 */
function _getRules({ fields }) {
  const { label, required } = getProp({ fields });
  const { verification } = fields;
  const { type, max_length, min_length } = verification;

  return {
    maxlength: max_length,
    rules: [
      validateRequired(type, required, `请${TIPS_TEXT}${label}！`),
      validateType(type, TIPS_TEXT),
      validateStrLen({ max_length, min_length }, TIPS_TEXT)
    ]
  };
}

/**
 * @export  获取input 类型表单属性
 * @param {*} { fields }
 * @return {*} prop
 */
export default function getTextareaProp({ fields }) {
  const rules = _getRules({ fields });

  const prop = getProp({ fields });

  return {
    ...prop,
    ...rules
  };
}
